<?php


namespace app\shop\model;

use app\BaseModel;

class AdminGoodsParameterValue extends BaseModel
{
    protected $name = 'longbing_card_goods_parameter_value';

    /**
     * @param $where
     * @param $goods_id
     * @功能说明:查询商品参数数据
     * @author lxj
     * @DataTime: 2020/5/19 16:24
     */
    public function getHistory($where,$goods_id){
        //判断商品是否开启参数
        $is_parameter = AdminGoods::where('id',$goods_id)->value('is_parameter');
        if(!$is_parameter){
            return null;
        }
        $list = $this->alias('a')->leftjoin('longbing_card_goods_parameter b','b.id=a.parameter_id')
            ->where($where)
            ->field('a.id,a.parameter_value,a.parameter_id,b.name parameter_name,b.pid')
            ->select()
            ->toArray();
        $data['info'] = self::getTree($list);
        return $data;
    }

    /**
     * @param $data
     * @功能说明:商品参数值新增
     * @author lxj
     * @DataTime: 2020/5/19 16:56
     */
    public function goodsParameterAdd($data){
        $data['create_time'] = time();
        $data['status'] = 1;
        $res = $this->insertGetId($data);
        return $res;
    }

    /**
     * @param $where
     * @功能说明:商品参数值删除
     * @author lxj
     * @DataTime: 2020/5/19 17:02
     */
    public function goodsParameterDel($where){
        $res = $this->where($where)->update(['status'=>-1]);
        return $res;
    }
    /**
     * @param array $arr
     * @param int $pid
     * @param int $step
     * @功能说明:数据树状处理
     * @author lxj
     * @DataTime: 2020/5/19 15:01
     */
    public function getTree($arr = [], $pid = 0, $step = 0)
    {
        $trees = [];
        $lists = array_values($arr);
        foreach ($lists as $key => $value) {
            if ($value['pid'] == $pid) {
                $child = self::getTree($lists, $value['parameter_id'], $step + 1);
                if (!empty($child)) {
                    $value['secondList'] = $child;
                }
                if($value['pid'] == 0){
                    $value['firstId'] = $value['parameter_id'];
                }else{
                    $value['id'] = $value['parameter_id'];
                }
                unset($value['parameter_id']);
                $trees[] = $value;
            }
        }
        return $trees;
    }

    /**
     * @param $where
     * @功能说明:查询数据是否存在
     * @author lxj
     * @DataTime: 2020/5/20 14:50
     */
    public function goodsParameterFind($where){
        $res = $this->where($where)->find();
        return $res;
    }

    /**
     * @param $where
     * @功能说明:查询参数是否被使用
     * @author lxj
     * @DataTime: 2020/5/21 20:25
     */
    public function parameterUse($where){
        $goods_id = $this->where($where)->column('goods_id');
        if($goods_id){
            //查询商品状态
            $goods_status = AdminGoods::where('id','in',$goods_id)->where(['status'=>1,'is_parameter'=>1])->find();
            if($goods_status){
                return true;
            }else{
                return false;
            }
        }else{
           return false;
        }

    }
}